package com.pengpeng.rocketmq.web;

import com.pengpeng.rocketmq.consumer.BroadcastConsumer;
import com.pengpeng.rocketmq.producer.BroadcastProducer;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.remoting.common.RemotingHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author 彭鹏  广播消息
 * @date 2021/10/25.
 */
@Slf4j
@RestController
@RequestMapping("/broadcast")
public class BroadcastController {

    @Autowired
    private BroadcastProducer producer;

    @RequestMapping("/broadcastProducer")
    public Object broadcastProducer() throws Exception {
        for (int i = 0; i < 15; i++) {
            Message msg = new Message(BroadcastConsumer.TOPIC,
                    "Broadcast1",
                    "OrderID111",
                    "Hello world".getBytes(RemotingHelper.DEFAULT_CHARSET));
            SendResult sendResult = producer.getProducer().send(msg);
            System.out.printf("%s%n", sendResult);
        }
        Thread.sleep(5000);
        return "成功";
    }
}

